package com.socool.setting;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.socool_login.Act_Login;
import com.example.socool_login.Act_Login_FB;
import com.example.socool_login.R;
import com.facebook.Session;
import com.socool.manager.Manager_Shared;

public class Act_Setting extends Activity {
	Button BT_logout;

	ImageView IV_push;
	ImageView IV_pushposition;

	ImageView IV_friend;
	ImageView IV_friendposition;
	TextView TV_friend;
	
	LinearLayout LL_coupon;
	LinearLayout LL_notice;
	LinearLayout LL_message;
	LinearLayout LL_blog;
	LinearLayout LL_faq;
	LinearLayout LL_help;
	LinearLayout LL_guide;

	TextView TV_count_message;
	TextView TV_count_notice;
	
	
	TextView TV_title;
	

	Manager_Shared mManagerShared;

	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.tab_setting);
		varInit();
		eventInit();
	}
	
	public void onResume(){
		super.onResume();
		
		
		if(mManagerShared.getPref_GCMStatus().equals("TRUE")){
			IV_push.setImageResource(R.drawable.on_bg);
			FrameLayout.LayoutParams localLayoutParams2 = (FrameLayout.LayoutParams) IV_pushposition.getLayoutParams();
			localLayoutParams2.gravity = 5;
			IV_pushposition.setLayoutParams(localLayoutParams2);
			
		}
		else{
			IV_push.setImageResource(R.drawable.off_bg);
			FrameLayout.LayoutParams localLayoutParams1 = (FrameLayout.LayoutParams) IV_pushposition.getLayoutParams();
			localLayoutParams1.gravity = 3;
			IV_pushposition.setLayoutParams(localLayoutParams1);
			
		}
	
		if(mManagerShared.getPref_RecommandStatus().equals("TRUE")){
			IV_friend.setImageResource(R.drawable.on_bg);
			FrameLayout.LayoutParams localLayoutParams2 = (FrameLayout.LayoutParams) IV_friendposition.getLayoutParams();
			localLayoutParams2.gravity = 5;
			IV_friendposition.setLayoutParams(localLayoutParams2);
			
		}
		else{
			IV_friend.setImageResource(R.drawable.off_bg);
			FrameLayout.LayoutParams localLayoutParams1 = (FrameLayout.LayoutParams) IV_friendposition.getLayoutParams();
			localLayoutParams1.gravity = 3;
			IV_friendposition.setLayoutParams(localLayoutParams1);
			
		}
			
		
	}
	
	

	private void connectPush(boolean paramBoolean) {
		if (paramBoolean) {
			mManagerShared.setPref_GCMStatus("TRUE");
			IV_push.setImageResource(R.drawable.on_bg);
			FrameLayout.LayoutParams localLayoutParams2 = (FrameLayout.LayoutParams) IV_pushposition.getLayoutParams();
			localLayoutParams2.gravity = 5;
			IV_pushposition.setLayoutParams(localLayoutParams2);
			TranslateAnimation localTranslateAnimation2 = new TranslateAnimation(1, -0.5F, 1, 0.0F, 2, 0.0F, 2, 0.0F);
			localTranslateAnimation2.setDuration(500L);
			IV_pushposition.startAnimation(localTranslateAnimation2);
		} else {
			mManagerShared.setPref_GCMStatus("FALSE");
			IV_push.setImageResource(R.drawable.off_bg);
			FrameLayout.LayoutParams localLayoutParams1 = (FrameLayout.LayoutParams) IV_pushposition.getLayoutParams();
			localLayoutParams1.gravity = 3;
			IV_pushposition.setLayoutParams(localLayoutParams1);
			TranslateAnimation localTranslateAnimation1 = new TranslateAnimation(1, 0.5F, 1, 0.0F, 2, 0.0F, 2, 0.0F);
			localTranslateAnimation1.setDuration(500L);
			IV_pushposition.startAnimation(localTranslateAnimation1);
		}
	}

	private void connectRecommand(boolean paramBoolean) {
		if (paramBoolean) {
			mManagerShared.setPref_RecommandStatus("TRUE");
			IV_friend.setImageResource(R.drawable.on_bg);
			FrameLayout.LayoutParams localLayoutParams2 = (FrameLayout.LayoutParams) IV_friendposition.getLayoutParams();
			localLayoutParams2.gravity = 5;
			IV_friendposition.setLayoutParams(localLayoutParams2);
			TranslateAnimation localTranslateAnimation2 = new TranslateAnimation(1, -0.5F, 1, 0.0F, 2, 0.0F, 2, 0.0F);
			localTranslateAnimation2.setDuration(500L);
			IV_friendposition.startAnimation(localTranslateAnimation2);
		} else {
			mManagerShared.setPref_RecommandStatus("FALSE");
			IV_friend.setImageResource(R.drawable.off_bg);
			FrameLayout.LayoutParams localLayoutParams1 = (FrameLayout.LayoutParams) IV_friendposition.getLayoutParams();
			localLayoutParams1.gravity = 3;
			IV_friendposition.setLayoutParams(localLayoutParams1);
			TranslateAnimation localTranslateAnimation1 = new TranslateAnimation(1, 0.5F, 1, 0.0F, 2, 0.0F, 2, 0.0F);
			localTranslateAnimation1.setDuration(500L);
			IV_friendposition.startAnimation(localTranslateAnimation1);
		}
	}

	private void eventInit() {

		IV_push.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

			/*	if (mManagerShared.getPref_GCMStatus().equals("TRUE"))
					connectPush(false);
				else
					connectPush(true);*/
				connectPush(mManagerShared.getPref_GCMStatus().equals("TRUE")? false : true);
				

			}
		});

		IV_friend.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				/*
				if (mManagerShared.getPref_RecommandStatus().equals("TRUE"))
					connectRecommand(false);
				else
					connectRecommand(true);
				 */
				connectRecommand(mManagerShared.getPref_RecommandStatus().equals("TRUE")? false: true);
			}
		
			
		});

		LL_coupon.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
		LL_notice.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
		LL_message.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
		LL_blog.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
		LL_faq.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
		LL_help.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
		LL_guide.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
		
		BT_logout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onClickLogout(); 
				 Intent intent = new Intent(Act_Setting.this, Act_Login.class);
				  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
			      startActivity(intent);
			      overridePendingTransition(R.anim.fade,R.anim.hold);
				
			}
		});
	}

	private void varInit() {
		mManagerShared = new Manager_Shared(getApplicationContext());

		LL_faq = ((LinearLayout) findViewById(R.id.LL_faq));
		LL_notice = ((LinearLayout) findViewById(R.id.LL_notice));
		LL_blog = ((LinearLayout) findViewById(R.id.LL_blog));

		LL_message = ((LinearLayout) findViewById(R.id.LL_message));
		LL_guide = ((LinearLayout) findViewById(R.id.LL_guide));
		LL_help = ((LinearLayout) findViewById(R.id.LL_help));
		LL_coupon = ((LinearLayout) findViewById(R.id.LL_coupon));

		TV_count_notice = ((TextView) findViewById(R.id.TV_count_notice));
		TV_count_message = ((TextView) findViewById(R.id.TV_count_message));

		IV_push = ((ImageView) findViewById(R.id.IV_push));
		IV_pushposition = ((ImageView) findViewById(R.id.IV_pushposition));

		IV_friend = ((ImageView) findViewById(R.id.IV_friend));
		IV_friendposition = ((ImageView) findViewById(R.id.IV_friendposition));

		TV_friend = (TextView)findViewById(R.id.TV_friend);
		TV_friend.post(new Runnable() {
			@Override
			public void run() {
				
				String str = getResources().getString(R.string.act_setting_friend_description);
				String text = breakText(TV_friend.getPaint(), str, TV_friend.getWidth()); 
				TV_friend.setText(text); 
			}
		});
		
		
		BT_logout = ((Button) findViewById(R.id.BT_logout));

	}
	
	
	public String breakText(Paint textPaint, String strText, int breakWidth) { 
		          StringBuilder sb = new StringBuilder(); 
		          int endValue = 0; 
		          do{ 
		              endValue = textPaint.breakText(strText, true, breakWidth, null); 
		              if(endValue > 0) { 
		                 sb.append(strText.substring(0, endValue)).append("\n"); 
		                  strText = strText.substring(endValue); 
		              } 
		          }while(endValue > 0); 
		          return sb.toString().substring(0, sb.length()-1); 
	} 
	
	 private void onClickLogout() {
	        Session session = Session.getActiveSession();
	        
	        if (!session.isClosed()) {
	            session.closeAndClearTokenInformation();
	        }
	    }
		



}
